package com.example.maze;

import receivers.RegisterRepReceiver;
import services.LoginService;
import services.RegisterService;
import tw.edu.tku.im.maze.*;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;

public class RegisterActivity extends Activity {
   
   
   //[ ---------- Life cycle ----------
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_register);
      
      RegisterRepReceiver mRegisterRepReceiver= new RegisterRepReceiver(this);
      LocalBroadcastManager.getInstance(this).registerReceiver(mRegisterRepReceiver,
                                                               new IntentFilter(RegisterService.BROADCAST_ACTION));
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_register, menu);
      return true;
   }
   //] ---------- Life cycle ----------
   MyProgressDialog mProgressDialog;
   public void showMyDialog(String title, String msg) {
      if(mProgressDialog == null) {
         mProgressDialog= new MyProgressDialog(this);
      }
      
      if(!mProgressDialog.hasShowed()) {
         mProgressDialog.show("","帳號驗證中, 請稍候");
      } else {
         mProgressDialog.dismiss();
      }
   }   
   public void dismissMyDialog() {
      if(mProgressDialog.hasShowed()) {
         mProgressDialog.dismiss();
      }
   }
   // -------------------------------------------------- 
   //[ 送出按鈕事件處理
   public void button_submitOnClicked(View view) {
      EditText editTextId= (EditText)this.findViewById(R.id.editText_userId);
      EditText editTextName= (EditText)this.findViewById(R.id.editText_userName);
      EditText editTextPwd= (EditText)this.findViewById(R.id.editText_userPwd);
      this.showMyDialog("", "帳號驗證中, 請稍候");
      
      //[ 開 service thread背景處理
      Intent mServiceIntent= new Intent(this, RegisterService.class);
      mServiceIntent.putExtra("id", editTextId.getText().toString());
      mServiceIntent.putExtra("name", editTextName.getText().toString()); 
      mServiceIntent.putExtra("pwd", editTextPwd.getText().toString());
      
      this.startService(mServiceIntent);
   }
   // -------------------------------------------------- 
   //[ 返回按鈕事件處理
   public void button_goBackOnClicked(View view) {
      Intent intent= new Intent(this, MainActivity.class);
      this.startActivity(intent);
   }
   // -------------------------------------------------- 
   //[ 處理帳號或密碼錯誤
   private void alertDialog(int externalStrResourseId) {
      //[ 系統訊息視窗
      AlertDialog.Builder builder = new AlertDialog.Builder(this);      
      builder.setPositiveButton(R.string.dialog_ok_ch, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) {
            // User clicked OK button
         }
      });         
      builder.setMessage(externalStrResourseId);
      AlertDialog dialog = builder.create();
      dialog.show();
   }
// --------------------------------------------------
   //[ 印出錯誤訊息
   public void showMsgDialog(int externalStrResourseId) {
      //[ 系統訊息視窗
      try {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setPositiveButton(R.string.dialog_ok_ch, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
               // User clicked OK button
            }
         });         
         //builder.setMessage(externalStrResourseId);
         builder.setMessage(externalStrResourseId);
         AlertDialog dialog = builder.create();
         if(dialog==null) MyLog.d("dialog==null", getClass().getName(), 45);
         dialog.show();
         MyLog.d("errorHandler6", getClass().getName(), 50); 
      } catch(Exception e) {
         MyLog.d("errorHandler7", e.getMessage(), 50); 
      }      
   }
}
